/**
 * Copyright (c) 2021 Coder League
 * All rights reserved.
 *
 * File：OrderDaoImpl.java
 * History:
 *         2021年1月27日: Initially created, Chrise.
 */
package club.coderleague.cfss.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import club.coderleague.cfss.dao.extension.OrderDaoExtension;
import club.coderleague.cfss.domain.beans.Order;
import club.coderleague.cfss.domain.entities.Orders;
import club.coderleague.cfss.domain.enums.EntityState;
import club.coderleague.cfss.util.CommonUtil;
import club.coderleague.data.jpa.domain.Page;
import club.coderleague.data.jpa.id.support.SnowflakeV4;
import club.coderleague.data.jpa.repository.support.AbstractDataRepositoryExtension;

/**
 * 订单数据访问对象实现。
 * @author Chrise
 */
public class OrderDaoImpl extends AbstractDataRepositoryExtension<Orders, Long, SnowflakeV4> implements OrderDaoExtension {
	/**
	 * @see club.coderleague.cfss.dao.extension.OrderDaoExtension#queryHandlingNumber(long, boolean)
	 */
	@Override
	public int queryHandlingNumber(long entId, boolean isSupplier) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("entid", entId);
		
		String jpql = "select count(entityid) from Orders where ";
		if (isSupplier) {
			params.put("state", EntityState.CONFIRMED.getValue());
			jpql += "supplierid = :entid and entitystate = :state";
		} else {
			params.put("state", EntityState.DISTRIBUTED.getValue());
			jpql += "cybercafeid = :entid and entitystate = :state";
		}
		
		return super.queryLong(jpql, params).intValue();
	}
	
	/**
	 * @see club.coderleague.cfss.dao.extension.OrderDaoExtension#queryOrderList(long, java.lang.Integer, boolean)
	 */
	@Override
	public List<Order> queryOrderList(long entId, Integer orderState, boolean isSupplier) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("entid", entId);
		
		String query = null, table = null, where = null;
		if (isSupplier) {
			query = ", c.entname";
			table = ", Cybercafes c ";
			where = "o.cybercafeid = c.entityid and o.supplierid = :entid ";
		} else {
			query = ", s.entname";
			table = ", Suppliers s ";
			where = "o.supplierid = s.entityid and o.cybercafeid = :entid ";
		}
		String sql = "select o.entityid as orderid, o.entitystate as orderstate" + query + ", o.ordertotal from Orders o" + table + "where " + where;
		
		if (orderState == null) {
			sql += "and o.entitystate != :entitystate ";
			params.put("entitystate", EntityState.DELETED.getValue());
		} else {
			sql += "and o.entitystate = :entitystate ";
			params.put("entitystate", orderState);
		}
		
		sql += "order by o.entityid desc";
		
		return super.queryCustomBeanListBySql(Order.class, sql, params);
	}
	
	/**
	 * @see club.coderleague.cfss.dao.extension.OrderDaoExtension#queryOrderDetail(long, boolean)
	 */
	@Override
	public Order queryOrderDetail(long orderid, boolean isSupplier) {
		String query = null, table = null, where = null;
		if (isSupplier) {
			query = ", c.entname, c.leader, c.contactnumber, concat(pn.areaname, cn.areaname, dn.areaname, sn.areaname, c.detailaddr) as address";
			table = ", Cybercafes c ";
			where = "o.cybercafeid = c.entityid and c.provinceid = pn.entityid and c.cityid = cn.entityid and c.districtid = dn.entityid and c.streetid = sn.entityid ";
		} else {
			query = ", s.entname, s.leader, s.contactnumber, concat(pn.areaname, cn.areaname, dn.areaname, sn.areaname, s.detailaddr) as address";
			table = ", Suppliers s ";
			where = "o.supplierid = s.entityid and s.provinceid = pn.entityid and s.cityid = cn.entityid and s.districtid = dn.entityid and s.streetid = sn.entityid ";
		}
		String sql = "select o.entityid as orderid, o.entitystate as orderstate" + query + ", o.ordertotal, o.confirmtime " + 
			"from Orders o, Areas pn, Areas cn, Areas dn, Areas sn" + table + 
			"where " + where + "and o.entityid = ?0";
		
		return super.queryCustomBeanBySql(Order.class, sql, orderid);
	}
	
	/**
	 * @see club.coderleague.cfss.dao.extension.OrderDaoExtension#queryListData(java.util.List, java.lang.String, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public Page<Map<String, Object>> queryListData(List<Integer> states, String keyword, Integer pageIndex, Integer pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		
		String csql = "select count(o.entityid) from Orders o, Cybercafes c, Suppliers s " 
			+ "where o.cybercafeid = c.entityid and o.supplierid = s.entityid and o.entitystate in (:entitystate)";
		String qsql = "select o.entityid, c.entname as centname, c.buslicenseno as cbuslicenseno, c.leader as cleader, c.contactnumber as ccontactnumber, " 
			+ "s.entname as sentname, s.buslicenseno as sbuslicenseno, s.leader as sleader, s.contactnumber as scontactnumber, " 
			+ "o.ordertotal, o.confirmtime, o.distributetime, o.completetime, o.entitystate " 
			+ "from Orders o, Cybercafes c, Suppliers s where o.cybercafeid = c.entityid and o.supplierid = s.entityid and o.entitystate in (:entitystate)";
		params.put("entitystate", states);
		
		if (CommonUtil.isNotEmpty(keyword)) {
			csql += " and (c.entname like :keyword or s.entname like :keyword)";
			qsql += " and (c.entname like :keyword or s.entname like :keyword)";
			params.put("keyword", "%" + keyword + "%");
		}
		
		qsql += " order by o.entityid desc";
		
		return super.queryMapPageBySql(csql, qsql, pageIndex, pageSize, params);
	}
}
